package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000122699136";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDTHUa9C9zxlrL8XwMDHHcNpaMPLwB+4daexTUMDzvPrQFosFzKINVFdBrIHR27QO/J24Y5csjJZQKl9XfJ6DEGv6SHgQNXXPZ0sO702wCJZO/rmGcgdrZgGTqtIS7JKYpJCUstUE51/TMKn9t0W6iynCu72UpvVhasQ4Hy0qVl3QrGaDyRq1jNWL2lDVPMvuGXcUSR9gaFWSm55DU7cwKGC6TKaiOfq7RSfDZFwYXqUK1TEaFbUtJcX9fvd6/aKoBdkRNXYPxLUgxuJKDolUDhtXIohOD4pWLuqVxVJ2NPP1u2Hrsqwh6cgYm7geTI+7Fc582Qvxx5ZdPk3ufcpOlzAgMBAAECggEALWaoZdN9Sn9MMrYEIdEnUUoDzSoggyjyTObAR72m8TyVhvwWmGmDNBKKB+srD7AhYVnSfdxPjfApJ6OohUVn9bmVGocGPu2BGwtJ+GJ4tRN9/T6jPvoIspX6Zm0ycJR8ZsSW5EHW/czcAzbprRZhKzgmt5k9RsBO0/EYqy3d4A/LN1/d2qfsWUPV0i6PLT2hsPeFDV4r3Mbcyh+E5RdLHut0m808l+E5O0qO9irhGo3q52dmewjmRjLNMrt0JK9yOzL7S/tS/v1e5tTIJbS+UPGG1Zsh3Xy+f5cii8CkMoUm0TFMPKDoSmPDlCEZo0V0/755dqXR/ROvRmQSbgJr8QKBgQD0z7I3Hz+aAInJWtnZa419SmntBQoT+Xwyt8TjJEzoDctEpPw5payIKCAvb4uTKiLRp6NxNcYCwNFBEHCU/cmKmASAe1TtMLvssm9rEXNjj8XzKnbGJIZY9lZ19we0TzLdXD2YVlHqWufyKWK7kcLa5QtesoRQ1UgIFrl19H5WrwKBgQDcw1MBqM6lmn3QsEZFYaaH9rFIIakeaapw8R2O9tmqnQpwEjcEE2Sddi3G9bWnFzzVuWk2muYwb9Xf+HtDsMHBl/jt/OA68gnK8O70CtizVMWdTihqX+YBSBsP37gBepfk7wnCtWTRIZgtBi0BihCw4jzTCfsjWxFUUpu2YfpKfQKBgQC3lFxnG4a9PHQWy4sbkJycfMXsGGBTVD3/ZnE9h8YoKdfpFPOTYxp7S+O+zp3S8MEFiPl2dsWS4PbKHMLf52RQCuktU87+wxHZUIFev8pQucrOK2S/9znL0oZJlcJwQwe4mWgTKX/qBttya/h/AbImJaYeEC5K03Vdo3TV8E8hPwKBgQC4KR0Col6Y2XMhdxt334k7nPixEptxKnartwiQba6Q3GaLrTZF6SOjE6YGdozt8b2nEQALNFbYi0LgjvnNMxO/OtbHOY+YvLacnDbKUI4Ls6kSm9OtQYivAwbITDnEyg8SRLJgwk9zAlJfJOrAgPuBg4l4B7h/+wVO0YbiM4PHRQKBgAHTdi9MYd9PpakIHrQfrPjmDLP1Ak60uWKsVoNTZ1X56C0qN+jSWFFXuh6f5/iiP26QFk+2/xkQhn4gtGHl70Oc6F6m0l/SAlBTHnqb+3QHaoR/HRcxuQQ7zKytMuKrA7QMzvHN1jbIiDE9NVPLV3nnB5I9M3uiM8oUNrubbkWv";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0x1GvQvc8Zay/F8DAxx3DaWjDy8AfuHWnsU1DA87z60BaLBcyiDVRXQayB0du0DvyduGOXLIyWUCpfV3yegxBr+kh4EDV1z2dLDu9NsAiWTv65hnIHa2YBk6rSEuySmKSQlLLVBOdf0zCp/bdFuospwru9lKb1YWrEOB8tKlZd0Kxmg8katYzVi9pQ1TzL7hl3FEkfYGhVkpueQ1O3MChgukymojn6u0Unw2RcGF6lCtUxGhW1LSXF/X73ev2iqAXZETV2D8S1IMbiSg6JVA4bVyKITg+KVi7qlcVSdjTz9bth67KsIenIGJu4HkyPuxXOfNkL8ceWXT5N7n3KTpcwIDAQAB";// 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://i2cbk7.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout>express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
